function z = find_gk(xx)
global N tauL tauK betta alpha depr sigmaC sigmaL popweight phi lam gassetrat e_real govery
g1=xx(1);
kj=xx(2:N);
omega=xx(N+1);
kg=xx(N+2);
depr=xx(N+3);
kss=(((1/betta-1)/(1-tauK)+depr)/alpha/e_real^(1-alpha))^(1/(alpha-1)); %the Euler at ss gives aggregate capital
css=(kss^alpha*e_real^(1-alpha)-g1-depr*kss)/(popweight(1)+sum(popweight(2:N).*lam)); %the resource constraint gives c1
%find l1 from optimality conditions of ratio of hours of different groups
%and using that average hours must match the data
for jj=2:N
ljpart(jj-1)=popweight(jj)*phi(jj)*lam(jj-1)^(-sigmaC/sigmaL)*(phi(jj)/phi(1))^(1/sigmaL);
end
l1ss=e_real/(popweight(1)*phi(1)+sum(ljpart));
k1ss=(css^(-sigmaC)*css-omega*l1ss^(sigmaL)*l1ss)/(1-betta)/css^(-sigmaC)/(1+(alpha*(e_real/kss)^(1-alpha)-depr)*(1-tauK)); %lifetime budget constraint of group 1
z(1)=omega*l1ss^(sigmaL)/css^(-sigmaC)-(1-alpha)*(kss/e_real)^alpha*(1-tauL); %consumption-leisure foc of group 1
for jj=2:N
z(jj)=(css^(-sigmaC)*lam(jj-1)*css-omega*l1ss^(sigmaL)*phi(jj)/phi(1)*L2(lam(jj-1),l1ss,phi(jj)))/(1-betta)...
    -css^(-sigmaC)*kj(jj-1)*(1+(alpha*(e_real/kss)^(1-alpha)-depr)*(1-tauK)); %lifetime budget constraint of group j
end
z(N+1)=kss-kg-popweight(1)*k1ss-popweight(2:N)*kj'; %weighted sum of groups' capital holdings must match aggregate capital
z(N+2)=kg/kss^alpha/e_real^(1-alpha)-gassetrat; %dept/GDP as in the data
z(N+3)=g1/kss^alpha/e_real^(1-alpha)-govery; %g/GDP as in the data



